package view;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GraphicsEnvironment;
import java.awt.Image;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.text.SimpleDateFormat;

import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.Timer;
import javax.swing.border.EtchedBorder;

import model.Account;
import model.BuyTableModel;
import model.DeviceTableModel;
import model.EmpTableModel;
import model.FormatFactory;
import model.SellTableModel;
import model.StatisticTableModel;
import model.StoreImages;
import model.SystemTableModel;
import model.UICommand;
import test.ModifiedFlowLayout;
import test.TestFont;

import com.alee.laf.button.WebButton;
import com.alee.laf.button.WebToggleButton;
import com.alee.laf.toolbar.WebToolBar;
import com.jgoodies.forms.factories.FormFactory;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.RowSpec;

import controller.BuyController;
import controller.DeviceController;
import controller.EmployeeController;
import controller.MainController;
import controller.SellController;
import controller.StatisticController;
import controller.SystemController;

public class MainFrame {
	private JFrame frame;
	private CardLayout card;
	private CardLayout mainCard;
	private JPanel view;
	private JLabel lblTime;
	private SimpleDateFormat dateFormat;
	private JPopupMenu popupMenu;
	private WebButton lblIcon;
	public static Account account;
	private MainController control;
	private Image image;

	/**
	 * Create the application.
	 */
	public MainFrame(MainController control) {
		this.control = control;
		frame = new JFrame("Store Manager");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setBounds(GraphicsEnvironment.getLocalGraphicsEnvironment()
				.getMaximumWindowBounds());
		frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
		mainCard = new CardLayout(0, 0);
		frame.getContentPane().setLayout(mainCard);
		// initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		// create popup menu
		// account = new Account("TEST", "TEST");
		popupMenu = new JPopupMenu();

		JMenuItem mntmngXut = new JMenuItem("Đăng xuất - ".concat(account
				.getName()));
		mntmngXut.setIcon(StoreImages.getIcon(StoreImages.LOGOUT_ICON));
		mntmngXut.setActionCommand(UICommand.LOGGOUT_CMD);
		// change UI to loggin panel
		mntmngXut.addActionListener(control);
		popupMenu.add(mntmngXut);

		JMenuItem mntmThotHThng = new JMenuItem("Thoát chương trình");
		mntmThotHThng.setIcon(StoreImages.getIcon(StoreImages.EXIT_ICON));
		mntmThotHThng.setActionCommand(UICommand.EXIT_CMD);
		mntmThotHThng.addActionListener(control);
		popupMenu.add(mntmThotHThng);
		popupMenu.pack();

		ButtonGroup group = new ButtonGroup();

		view = new JPanel();
		card = new CardLayout(0, 0);
		view.setLayout(card);

		JTabbedPane hoaDon = new JTabbedPane(JTabbedPane.TOP);
		hoaDon.setFocusable(false);
		view.add(hoaDon, UICommand.BILL_PANEL);

		// TODO create panel
		SellTableModel sellModel = new SellTableModel();
		SellController sellControl = new SellController(sellModel);
		hoaDon.addTab("Bán hàng", StoreImages.getIcon(StoreImages.SELLUP_ICON),
				sellControl.getView(), null);

		// MVC
		BuyTableModel buyModel = new BuyTableModel();
		BuyController buyControl = new BuyController(buyModel);
		hoaDon.addTab("Mua hàng",
				StoreImages.getIcon(StoreImages.SELLDOWN_ICON),
				buyControl.getView(), null);

		// panel employee
		EmpTableModel emplModel = new EmpTableModel();
		EmployeeController emplControl = new EmployeeController(emplModel);
		view.add(emplControl.getView(), UICommand.EMPLOYEE_PANEL);

		// panel statistic
		StatisticTableModel staModel = new StatisticTableModel();
		StatisticController staControl = new StatisticController(staModel);
		view.add(staControl.getView(), UICommand.STATISTIC_PANEL);

		// panel warehouse
		DeviceTableModel deviceModel = new DeviceTableModel();
		DeviceController deviceControl = new DeviceController(deviceModel);
		view.add(deviceControl.getView(), UICommand.DEVICE_PANEL);

		// panel system account
		SystemTableModel systemModel = new SystemTableModel();
		SystemController accControl = new SystemController(systemModel);
		view.add(accControl.getView(), UICommand.SYSTEM_PANEL);

		JScrollPane luuTru = new JScrollPane();
		luuTru.setViewportView(new SavingPanel());
		view.add(luuTru, UICommand.LUU_TRU_PANEL);

		JPanel panel_1 = new JPanel();
		panel_1.setBorder(new EtchedBorder(EtchedBorder.RAISED, null, null));
		panel_1.setLayout(new FormLayout(
				new ColumnSpec[] { ColumnSpec.decode("default:grow"),
						FormFactory.RELATED_GAP_COLSPEC,
						ColumnSpec.decode("right:pref"),
						FormFactory.RELATED_GAP_COLSPEC,
						FormFactory.DEFAULT_COLSPEC, },
				new RowSpec[] { FormFactory.DEFAULT_ROWSPEC, }));

		// FIXME
		JPanel workspace = new JPanel(new BorderLayout()) {
			private static final long serialVersionUID = 1L;

			@Override
			protected void paintComponent(Graphics g) {
				super.paintComponent(g);
				Dimension d = getSize();
				g.drawImage(image, 0, 0, d.width, d.height, null);
			}
		};
		workspace.add(view, BorderLayout.CENTER);
		workspace.add(panel_1, BorderLayout.NORTH);
		frame.getContentPane().add(workspace, "workspace");

		// TODO create tool bar
		WebToolBar toolBar = new WebToolBar();
		toolBar.setLayout(new ModifiedFlowLayout());
		panel_1.add(toolBar, "1, 1");
		toolBar.setUndecorated(true);
		toolBar.setRollover(true);
		toolBar.setSpacing(-2);
		toolBar.setShadeWidth(0);
		toolBar.setFloatable(false);

		WebToggleButton tglbtnLpHBn = FormatFactory.createToggleButton(
				"Hoá đơn", "", UICommand.BILL_PANEL,
				StoreImages.getIcon(StoreImages.BILL_ICON), control);
		tglbtnLpHBn.setSelected(true);
		toolBar.add(tglbtnLpHBn);
		group.add(tglbtnLpHBn);

		WebToggleButton tglbtnLpHMua = FormatFactory.createToggleButton(
				"Nhân viên", "", UICommand.EMPLOYEE_PANEL,
				StoreImages.getIcon(StoreImages.EMPLOYEE_ICON), control);
		toolBar.add(tglbtnLpHMua);
		group.add(tglbtnLpHMua);

		WebToggleButton tglbtnThngK = FormatFactory.createToggleButton(
				"Thống kê", "", UICommand.STATISTIC_PANEL,
				StoreImages.getIcon(StoreImages.CHART_ICON), control);
		toolBar.add(tglbtnThngK);
		group.add(tglbtnThngK);

		WebToggleButton tglbtnKhoHng = FormatFactory.createToggleButton(
				"Kho hàng", "", UICommand.DEVICE_PANEL,
				StoreImages.getIcon(StoreImages.STORE_ICON), control);
		toolBar.add(tglbtnKhoHng);
		group.add(tglbtnKhoHng);

		WebToggleButton tglbtnLuTr = FormatFactory.createToggleButton(
				"Lưu trữ", "", UICommand.LUU_TRU_PANEL,
				StoreImages.getIcon(StoreImages.REPORT_ICON), control);
		tglbtnLuTr.setEnabled(false);
		toolBar.add(tglbtnLuTr);
		group.add(tglbtnLuTr);

		WebToggleButton tglbtnHThng = FormatFactory.createToggleButton(
				"Hệ thống", "", UICommand.SYSTEM_PANEL,
				StoreImages.getIcon(StoreImages.SYS_ICON), control);
		toolBar.add(tglbtnHThng);
		group.add(tglbtnHThng);

		lblTime = new JLabel("00:00:00");
		panel_1.add(lblTime, "3, 1");
		lblTime.setForeground(SystemColor.textHighlight);
		lblTime.setFont(TestFont.createFont(Font.PLAIN, 15));

		dateFormat = new SimpleDateFormat("HH:mm:ss");

		ActionListener action = new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				lblTime.setText(dateFormat.format(System.currentTimeMillis()));
			}
		};
		Timer timer = new Timer(1000, action);
		timer.start();

		// TODO button icon
		lblIcon = new WebButton();
		lblIcon.setRolloverDecoratedOnly(true);
		lblIcon.setIcon(StoreImages.getIcon(StoreImages.ICON));
		lblIcon.setActionCommand(UICommand.POPUP_CMD);
		lblIcon.addActionListener(control);
		panel_1.add(lblIcon, "5, 1");

		frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		frame.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				MainFrame.this.control.exit();
			}
		});

		// SystemPanel.setOpaque((JComponent) frame.getContentPane(), false);
	}

	public void showPopupMenu() {
		popupMenu.show(lblIcon,
				(int) (lblIcon.getAlignmentX() + lblIcon.getWidth() - popupMenu
						.getPreferredSize().width),
				(int) lblIcon.getAlignmentY() + lblIcon.getHeight());
	}

	// change panel manager
	public void showComponent(String name) {
		card.show(view, name);
	}

	// TODO change display between workspace and loggin panel
	public void changeDisplay(String name) {
		mainCard.show(frame.getContentPane(), name);
	}

	public void display(Account account, Image img) {
		this.image = img;
		MainFrame.account = account;
		initialize();
	}

	public void addComponent(JPanel panel, String name) {
		this.frame.getContentPane().add(panel, name);
	}

	public void setVisible(boolean b) {
		this.frame.setVisible(b);
	}

	public Component getFrame() {
		return this.frame;
	}

}
